#include "xmlparsedefs.h"

XMLParseDefs::XMLParseDefs()
{
metatag = false;
worksheet = QLatin1String("");
}

XMLParseDefs::~XMLParseDefs()
{
}


bool XMLParseDefs::startElement( const QString & ns,
                                 const QString & localName,
                                 const QString & qName,
                                 const QXmlAttributes & attributes )
{
    if ( !metatag && true != qName.contains("Workbook") )
    {
    errorStr = QObject::tr("The file is not a MS XML Speadsheet file.");
    return false;
    }

    if ( qName == "Workbook" ||
         localName == "Workbook" )
    {
    metatag = true;
    }

    if ( ns != "urn:schemas-microsoft-com:office:spreadsheet" )
    {
    return true;
    }

    if ( qName == "Worksheet" ||
         localName == "Worksheet" )
    {
    m.clear();
    l.clear();

        if ( ( attributes.value("ss:Name").isNull() ||
               attributes.value("ss:Name").isEmpty() )
            &&
             ( attributes.value("Name").isNull() ||
               attributes.value("Name").isEmpty() ) )
        {
        worksheet = QLatin1String("");
        return true;
        }
        
        if ( true != attributes.value("ss:Name").isNull() &&
             true != attributes.value("ss:Name").isEmpty() )
        {
        worksheet = attributes.value("ss:Name");
        }
        else if ( true != attributes.value("Name").isNull() &&
                  true != attributes.value("Name").isEmpty() )
        {
        worksheet = attributes.value("Name");
        }
        else
        {
        worksheet = QLatin1String("");
        }
    }
    else
    {
    ;;
    }

currentText.clear();
return true;
}

bool XMLParseDefs::endElement( const QString & ns,
                               const QString & localName,
                               const QString & qName )
{
    if ( ns != "urn:schemas-microsoft-com:office:spreadsheet" )
    {
    return true;
    }

    if ( qName == "Worksheet" ||
         localName == "Worksheet" )
    {
    m[worksheet] = l;
    d.push_back(m);
    l.clear();
    m.clear();
    worksheet = QLatin1String("");
    }
    else if ( qName == "Data" ||
              localName == "Data" )
    {
    l << currentText;
    }
    else
    {
    ;;
    }
return true;
}

bool XMLParseDefs::characters(const QString &str)
{
currentText += str;
return true;
}

bool XMLParseDefs::fatalError(const QXmlParseException &exception)
{
errorStr = QObject::tr( "Parse error at line %1, column %2:\n %3")
                          .arg(exception.lineNumber())
                          .arg(exception.columnNumber())
                          .arg(exception.message() );
return false;
}

QString XMLParseDefs::errorString() const
{
return errorStr;
}

QList< QMap<QString,QStringList> > XMLParseDefs::return_data()
{
return d;
}
